package b2.widget.login
{
	import b2.config.*;
	import b2.net.*;
	import b2.scene.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.warehouse.*;
	
	import com.greensock.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.text.*;
	import flash.ui.Mouse;

	public class GiftItem extends Sprite
	{
		public var objectIndex:int;
		public var objBasicLevel:int;
		public var objStrengthenLevel:int;
		public var objNum:int;
		
		private var objects:Array;
		private var tip:Sprite;
		private var questionMarkContainer:Sprite;
		private var nameText:TextField;
		private var numText:TextField;
		
		public function GiftItem(objects:Array)
		{
			super();
			this.objects = objects;
			//背景框
			var GiftItemBgr:Class = ResourceHelper.getInstance().getClassByName("GiftItemBgr");
			var giftItemBgr:Sprite = new GiftItemBgr();
			addChild(giftItemBgr);
			//名字
			nameText = new TextField();
			nameText.selectable = false;
        	nameText.autoSize = TextFieldAutoSize.CENTER;
			nameText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0xFFFF00);
			nameText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			nameText.x = 90;
			nameText.y = 12;
			addChild(nameText);
			//数量
			numText = new TextField();
			numText.selectable = false;
        	numText.autoSize = TextFieldAutoSize.CENTER;
			numText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			numText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			numText.x = 90;
			numText.y = 32;
			addChild(numText);
			if (objects.length > 1)
			{
				//物品图片
				questionMarkContainer = new Sprite();
				var QuestionMark:Class = ResourceHelper.getInstance().getClassByName("QuestionMark");
				var questionMark:Sprite = new QuestionMark();
				questionMark.width = 50;
				questionMark.height = 50;
				questionMark.x = -questionMark.width / 2;
				questionMark.y = -questionMark.height / 2;
				questionMarkContainer.x = questionMark.width / 2 + 10;
				questionMarkContainer.y = questionMark.height / 2 + 8;
				questionMarkContainer.addChild(questionMark);
				addChild(questionMarkContainer);
				questionMark.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				questionMark.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				nameText.text = "未知";
			}
			else if (objects.length == 1)
			{
				var objectInfoStrs:Array = objects[0].split(",");
				objectIndex = objectInfoStrs[0];
				objBasicLevel = objectInfoStrs[1] / 5;
				objStrengthenLevel = objectInfoStrs[1] % 5;
				objNum = objectInfoStrs[2];
				var objectItem:ObjectItem = new ObjectItem(objectIndex, objBasicLevel, objStrengthenLevel, 0, objNum, -1, false);
				objectItem.x = 10;
				objectItem.y = 8;
				addChild(objectItem);
				var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
				nameText.text = objectConfig.name;
				numText.text = "*" + objNum;
			}
		}
		
		private function createTip():Sprite
		{
			var tip:Sprite = new Sprite;
			var roundRecContainer:Sprite = new Sprite();
			//名字
			var nameText:TextField = new TextField();
			nameText.selectable = false;
        	nameText.autoSize = TextFieldAutoSize.LEFT;
        	var nameTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			nameText.defaultTextFormat = nameTexFormat;
			nameText.text = "未知";
			roundRecContainer.addChild(nameText);
			//具体信息
			var tipText:TextField = new TextField();
			tipText.selectable = false;
        	tipText.autoSize = TextFieldAutoSize.LEFT;
        	var texFormat:TextFormat = new TextFormat("微软雅黑", 13, 0x000000);
			tipText.defaultTextFormat = texFormat;
			tipText.y = 20;
			for (var i:int = 0; i < objects.length; i++)
			{
				var objectStrs:Array = objects[i].split(",");
				var objectIndex:int = objectStrs[0];
				var objectName:String = Config.objectConfigs[objectIndex].name;
//				var objectBasicLevel:int = objectStrs[1] / 5;
//				var objectStrengthLevel:int = objectStrs[1] % 5;
				var objectNum:int = objectStrs[2];
				var objectPossability:int = objectStrs[3];
				tipText.appendText(objectName + "\t几率: " + (objectPossability / 10).toFixed(1) + "%\n");
			}
			roundRecContainer.addChild(tipText);
			roundRecContainer.x = 10;
			roundRecContainer.y = 10;
			var roundRec:RoundRec = new RoundRec(200, roundRecContainer.height + 30, 0.8, BFont.roundRecColor);
			tip.addChild(roundRec);
			roundRec.addChild(roundRecContainer);
			return tip;
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
//			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			if (tip == null)
			{
				var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
				tip = createTip(); 
				tip.x = globalPoint.x + 60;
				tip.y = globalPoint.y + 8;
				SceneManager.getInstance().currentScene.addChild(tip);
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
//			this.filters = null;
			if (tip != null && tip.parent != null)
			{
				tip.parent.removeChild(tip);
				tip = null;
			}
		}
		
		public function determine():void
		{
			if (objects.length > 1)
			{
				var rand:Number = Math.random();
				var accPossability:int = 0;
				for (var i:int = 0; i < objects.length; i++)
				{
					var objectStrs:Array = objects[i].split(",");
					var objectPossability:int = objectStrs[3];
					
					accPossability += objectPossability;
					if (rand <= accPossability / 1000 || i == objects.length - 1)
					{
						objectIndex = objectStrs[0];
						objBasicLevel = objectStrs[1] / 5;
						objStrengthenLevel = objectStrs[1] % 5;
						objNum = objectStrs[2];
						var objectContainer:Sprite = new Sprite;
						var objectItem:ObjectItem = new ObjectItem(objectIndex, objBasicLevel, objStrengthenLevel, 0, objNum, -1, false);
						objectItem.x = -objectItem.width / 2;
						objectItem.y = -objectItem.height / 2;
						objectContainer.addChild(objectItem);
						objectContainer.x = objectItem.width / 2 + 10;
						objectContainer.y = objectItem.height / 2 + 8;
						objectContainer.scaleX = 0;
						addChild(objectContainer);
						var flipTimeline:TimelineLite = new TimelineLite();
						flipTimeline.append(new TweenLite(questionMarkContainer, 0.2, {scaleX:0}));
						flipTimeline.append(new TweenLite(objectContainer, 0.2, {scaleX:1}));
						var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
						nameText.text = objectConfig.name;
						numText.text = "*" + objNum;
						break;
					}
				}
			}
		}
		
	}
}